package com.wondertek.web.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wondertek.common.base.Result;
import com.wondertek.common.log.AutoLog;
import com.wondertek.common.util.security.LoginUser;
import com.wondertek.common.util.security.PreAuthorize;
import com.wondertek.common.util.security.SecurityUtil;
import com.wondertek.service.dto.sys.req.*;
import com.wondertek.service.dto.sys.res.SysUserRes;
import com.wondertek.service.service.sys.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xqs
 */
@RestController
@RequestMapping("/backend/sys/user")
@Tag(name = "系统用户管理")
public class SysUserController {

    @Resource
    private ISysUserService sysUserService;

    /**
     * 分页查询用户
     *
     * @param sysUserQueryReq /
     * @return /
     */

    //@PreAuthorize("@ss.hasPermi('测试')")
    @PostMapping("/page")
    @Operation(summary = "分页查询用户")
    @PreAuthorize("@el.check('backend:sys:user:page','backend:business:site:page')")
    public Result<Page<SysUserRes>> page(@RequestBody SysUserQueryReq sysUserQueryReq) {
        return Result.success(sysUserService.page(sysUserQueryReq));
    }

    /**
     * 用户导出
     *
     * @param sysUserQueryReq
     */
    @PostMapping("/export")
    @Operation(summary = "用户导出")
    public void export(@RequestBody SysUserQueryReq sysUserQueryReq, HttpServletResponse response) {

        sysUserService.export(sysUserQueryReq, response);
    }

    /**
     * 新增用户
     *
     * @param sysUserAddReq /
     * @return /
     */
    @PostMapping("/add")
    @Operation(summary = "新增用户")
    @AutoLog(category = "sys", handleType = "log.user.insert",
            handleNote = "'新增用户:'+#sysUserAddReq.nickname")
    @PreAuthorize("@el.check('backend:sys:user:add')")
    public Result<?> add(@RequestBody @Validated SysUserAddReq sysUserAddReq) {
        sysUserService.add(sysUserAddReq);
        return Result.success();
    }

    /**
     * 修改用户
     *
     * @param sysUserUpdateReq /
     * @return /
     */
    @PostMapping("/update")
    @Operation(summary = "修改用户")
    @AutoLog(category = "sys", handleType = "log.user.update",
            handleNote = "'修改用户:'+#sysUserUpdateReq.nickname")
    @PreAuthorize("@el.check('backend:sys:user:update')")
    public Result<?> update(@RequestBody @Validated SysUserUpdateReq sysUserUpdateReq) {
        sysUserService.update(sysUserUpdateReq);
        return Result.success();
    }

    /**
     * 禁用用户
     *
     * @param baseUpdateReq /
     * @return /
     */
    @PostMapping("/disable")
    @Operation(summary = "禁用用户")
    @AutoLog(category = "sys", handleType = "log.user.disable",
            handleNote = "'禁用用户:'+@sysUserService.getById(#baseUpdateReq.id).getNickname()")
    @PreAuthorize("@el.check('backend:sys:user:disable')")
    public Result<?> disable(@RequestBody @Validated BaseUpdateReq baseUpdateReq) {
        sysUserService.disable(baseUpdateReq.getId());
        return Result.success();
    }

    /**
     * 启用用户
     *
     * @param baseUpdateReq /
     * @return /
     */
    @PostMapping("/enable")
    @Operation(summary = "启用用户")
    @AutoLog(category = "sys", handleType = "log.user.enable",
            handleNote = "'启用用户:'+@sysUserService.getById(#baseUpdateReq.id).getNickname()")
    @PreAuthorize("@el.check('backend:sys:user:enable')")
    public Result<?> enable(@RequestBody @Validated BaseUpdateReq baseUpdateReq) {
        sysUserService.enable(baseUpdateReq.getId());
        return Result.success();
    }

    /**
     * 修改密码
     *
     * @param updatePasswordReq /
     * @return /
     */
    @PostMapping("/updatePassword")
    @Operation(summary = "修改密码")
    @AutoLog(category = "sys", handleType = "log.user.update.password",
            handleNote = "'修改密码:'+@securityUtil.getLoginUser().getUsername()+'账户'")
    public Result<?> updatePassword(@RequestBody @Validated SysUserUpdatePasswordReq updatePasswordReq) {
        sysUserService.updatePassword(updatePasswordReq);
        return Result.success();
    }

    /**
     * 重置密码
     *
     * @param restPasswordReq /
     * @return /
     */
    @PostMapping("/restPassword")
    @Operation(summary = "重置密码")
    @AutoLog(category = "sys", handleType = "log.user.reset.password",
            handleNote = "'重置密码:'+@sysUserService.getById(#restPasswordReq.userId).getNickname()")
    @PreAuthorize("@el.check('backend:sys:user:restPassword')")
    public Result<?> restPassword(@RequestBody @Validated SysUserRestPasswordReq restPasswordReq) {
        sysUserService.restPassword(restPasswordReq);
        return Result.success();
    }

    /**
     * 查询用户的按钮权限
     *
     * @return /
     */
    @GetMapping("/buttonPermission")
    @Operation(summary = "查询用户的按钮权限")
    public Result<?> buttonPermission() {
        LoginUser loginUser = SecurityUtil.getLoginUser();
        List<String> list = loginUser.getAuthoritiesButton().stream().collect(Collectors.toList());
        return Result.success(list);
    }


    /**
     * 删除用户
     *
     * @return /
     */
    @PostMapping("/delete")
    @Operation(summary = "删除用户")
    @AutoLog(category = "sys", handleType = "log.user.delete",
            handleNote = "'删除用户'")
    @PreAuthorize("@el.check('backend:sys:user:delete')")
    public Result<?> delete(@RequestBody @Validated BaseUpdateReq baseUpdateReq) {
        sysUserService.delete(baseUpdateReq.getId());
        return Result.success();
    }


}
